param(	
	[string]$csvSource = "$(Read-Host 'Enter import csv file')",
	[string]$sourceFolderGats ="$(Read-Host 'Enter GATs PDF folder')",
	[string]$sourceFolderCa ="$(Read-Host 'Enter Credit Application PDF folder')"
	#[string]$gatWebUrl ="$(Read-Host 'Enter Temp folder')",	
	#[string]$caWebUrl ="$(Read-Host 'Enter Temp folder')"
)

Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
Write-Host "`n===================`n# Starting Import #`n==================="

#Load variables from configuration file
[System.Xml.XmlDocument] $xmlDoc = New-Object System.Xml.XmlDocument
$file = Resolve-Path (".\deployment.config")

if(!$file){
	Write-Host "Could not find the configuration file deployment.cnofig. Aborting." -ForegroundColor Red
	break
}

Write-Host "Parsing file: " $file
$xmlDoc = [xml](Get-Content $file)

$gatWebUrl = $xmlDoc.Configuration.WebApplication.SiteCollection.GATSSite.Url
$caWebUrl = $xmlDoc.Configuration.WebApplication.SiteCollection.CASite.Url




$gatListName = "GATs"
$caListName = "CreditApplications"
$documentType = "DocumentType"
$documentDate = "DocumentDate"
$customerName = "CustomerName"
$customerNumber = "CustomerNumber"

#Load the CSV file
Write-Host "Loading csv file"
Write-Host $csvSource
#$csvSource = $csvSource #'D:\Document Types\GAT\FinanceDocExport_18_05_2012_edit2.csv'
$csvLength = @(Get-Content $csvSource).Length
$csv = Import-Csv $csvSource
Write-Host "CSV load completed" -ForegroundColor Green

#Store the source folder
#$sourceFolderGats = $sourceFolderGats # 'D:\Document Types\GAT\2012\'
#$sourceFolderCredit = $sourceFolderCa # 'D:\Document Types\Credit Application\2012\'
#Load the site
$webGats = Get-SPWeb -identity $gatWebUrl # "http://turnersdms/gats/"
Write-Host "GATs Site loaded"
$webCredit = Get-SPWeb -identity $caWebUrl #"http://turnersdms/creditapplications/"
Write-Host "Credit Applications Site loaded"

#Load the document library
$listGats = $webGats.Lists[$gatListName]
$spFolderGats = $webGats.GetFolder($gatListName)
$spFileCollectionGats = $spFolderGats.Files
Write-Host "GATs Document library loaded"
$listCredit = $webCredit.Lists[$caListName]
$spFolderCredit = $webCredit.GetFolder($caListName)
$spFileCollectionCredit = $spFolderCredit.Files
Write-Host "Credit Applications Document library loaded"

#Assign folder
$currentFolderGats = $listGats.RootFolder
$currentFolderCredit = $listCredit.RootFolder

$itemCount = 0

foreach ($row in $csv){
	
	$itemCount++
	
	$url = $row."URL"
	$urlPieces = $url.Split("/");
	$urlPiecesCount = $urlPieces.Count
	$fileName = $urlPieces[$urlPiecesCount-1]
	
	
	#Set source folder according to document type
	if($row."Doc Type" -eq "GAT"){
		$sourceFolder = $sourceFolderGats
		$currentFolder = $listGats.RootFolder
	}
	if($row."Doc Type" -eq "Credit Application"){
		$sourceFolder = $sourceFolderCa
		$currentFolder = $listCredit.RootFolder
	}
	#Write-Host "Source Folder: $sourceFolder Destination Folder: $currentFolder"
	
	$filePath = $sourceFolder + '/' + $fileName
	#Write-Host "File Path: $filePath"
	
	try
	{
		#Write-Host "Opening File Stream"
		$fileStream = ([System.IO.FileInfo] (Get-Item $filePath)).OpenRead()
		
		#Write-Host "Adding file"
		$spFile = $currentFolder.Files.Add($currentFolder.Url + "/" + $fileName, [System.IO.Stream] $fileStream, $true)

		#Write-Host "Adding columns from CSV"
		$item = $spFile.Item
		#Write-Host "Writing Doc Type"
		$item[$documentType] = $row."Doc Type"
		
		#Write-Host "Writing Document Date"
		$date = $row."Document Date"
		$dateSplit = $date.Split("/")
		$newDate = $dateSplit[2].Split(" ")[0] + "-" + $dateSplit[1] + "-" + $dateSplit[0] + " " + "00:00:00"
		#Write-Host $date " Changed to " $newDate
		$item[$documentDate] = $newDate
		
		#Write-Host "Writing File Name"
		$item["Title"] = $fileName
		#Write-Host "Writing Customer Name"
		$item[$customerName] = $row."Customer Name"
		#Write-Host "Writing Customer Number"
		$item[$customerNumber] = $row."Customer No"
		$item.Update()
		#Write-Host "Success`n=================="
		Write-Progress -Activity "Import Files" -status "Progress: $itemCount/$csvLength" -PercentComplete (($itemCount/$csvLength)*100)
		
	}
	catch
	{
		Write-Host "Error adding file: " $filePath"`n"$_ -foregroundcolor red
	}
	
}
Write-Host "`n=================`nImport Completed.`nFiles Added: $itemCount.`nDuplicates Created: $duplicateCount.`n=================`n"

Remove-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
